<?php
// TODO: song    用户中心     2016/10/21
namespace Home\Controller;
use Think\Controller;
use Home\Common\Api\images;
use Home\Common\Api\UserApi;
class UsercenterController extends BaseController {
     public $USER = null;
     public $IMG = null;

      public function _initialize(){
        parent::_initialize();
          $this->USER = new UserApi();
          $this->IMG = new Images();
      }
  //用户中心获取模板接口调用
    public function index(){
      $res = array('status'=>'0','msg'=>'参数传递错误');
      $code = empty($_REQUEST['code'])?'userlist':$_REQUEST['code'];
      $pagenow = empty($_REQUEST['p'])?1:$_REQUEST['p'];
      $pagesize = empty($_REQUEST['pagesize'])?8:$_REQUEST['pagesize'];

      $ary = array('code'=>$code,'pagenow'=>$pagenow,'pagesize'=>$pagesize);
      $USERPATH= 'Home/Common/User/'.strtolower($code).'.class.php';
      //$file = __ROOT__.'/'.'Home/Common/User/'.strtolower($code).'.class.php';
      $file_name = strtolower($code).'.class.php';
      //var_dump($_SERVER);exit;
      $file_dir = $_SERVER ['DOCUMENT_ROOT'] .'/Home/Common/User/';
      if (!file_exists($file_dir . $file_name)) { //检查文件是否存在
          $res = array('status'=>'0','msg'=>'该方法不存在!');
          $this->ajaxReturn($res);
          exit;
      }
      require_once($USERPATH);
      $clsname='Home\\Common\\User\\'.$code;
      $sc = new $clsname();
      $jsondata= $sc->show($ary);

      if(!empty($_REQUEST['p'])){
        $this->ajaxReturn($jsondata);exit;
      }
      $this->assign('code',$code);
      $this->assign('list',$jsondata['list']);
      $this->assign('page', json_encode($jsondata['page']));
      $this->display('Usercenter/'.$code);
    }

    //用户中心首页
    public function usercenter(){
      $this->assign('action',I('get.action'));
      $this->display('index');
    }


      public function upload_img(){

       if (empty($_FILES)) {
          echo  "没有要上传的图片";
      }

      $res = $this->IMG->upload_img();
      print_r($res);
        
      }

//获取用户信息
public function get_user_list(){
   $res = array('status'=>'0','msg'=>'没有该用户资料!');

   $list = $this->USER->get_user_list();
  if(!$list){
    $this->ajaxReturn($res);
  }
   $res = array('status'=>'1','msg'=>'用户信息获取成功','row'=>$list);
   $this->ajaxReturn($res);
}

//更新用户信息
public function save_user_list(){
   $res = array('status'=>'0','msg'=>'参数传递错误!');
   $data = $_POST;
   if(!$data){
     $this->ajaxReturn($res);
   }
   
   $list = $this->USER->save_user_list($data);
  if(!$list){
     $res = array('status'=>'0','msg'=>'数据更新失败!');
    $this->ajaxReturn($res);
  }
   $res = array('status'=>'1','msg'=>'用户信息更新成功');
   $this->ajaxReturn($res);
}

//验证用户原密码是否正确
public function is_pwd_true(){
  $res = array('state'=>'0','msg'=>'参数传递错误!');
  $pwd = $_POST['pwd'];
   $row = $this->USER->is_pwd_true($pwd);
   if($row==flase){
      $res = array('state'=>'0','msg'=>'原密码错误!');
      $this->ajaxReturn($res);
   }
   $res = array('state'=>'1','msg'=>'密码正确');
   $this->ajaxReturn($res);
}

//密码重置、发邮件给用户邮箱去重置密码
public function reset_pwd(){
  $row = $this->USER->reset_pwd();
  //获取到邮箱,判断跳转地址

  $this->ajaxReturn($row);

  //https://mail.qq.com  跳转到改地址去
}


//修改头像上传图片
public function upload_img_header(){
    if (empty($_FILES)) {
             echo  "没有要上传的图片";
         }

         $res = $this->IMG->one_upload_img();
         $this->ajaxReturn($res);

}

//删除图片
public function del_img(){
     $ids = $_POST['ids'];
     if(empty($ids)){
      $this->ajaxReturn(array('state'=>0,'msg'=>'参数传递错误'));
     }
     $res = $this->IMG->del_img($ids);
     if($res){
        $this->ajaxReturn(array('state'=>1,'msg'=>'删除成功'));
     }
    

}


//显示相册相册
public function sel_photo(){
   $res = $this->IMG->sel_photo();
   if(!empty($res)){
     $this->ajaxReturn($res);
   }

}
//添加相册
public function add_photo(){
    $data = $_POST;
     $res = $this->IMG->add_photo($data);
     if($res){
      $this->ajaxReturn(array('state'=>1,'msg'=>'创建相册成功'));
     }


}
//修改相册资料
public function upd_photo(){
  

}
//删除相册
public function del_photo(){

}
//相册添加图片(在里面选择图片)
public function photo_add_img(){


} 
//相册添加图片(直接在相册里面上传图片)
public function photo_add_img2(){
    $type_id = $_REQUEST['type_id'];
    if(empty($type_id)){
       $this->ajaxReturn(array('state'=>0,'msg'=>'参数传递错误'));
    }
    if (empty($_FILES)) {
         $this->ajaxReturn(array('state'=>0,'msg'=>'没有要上传的图片'));
      }

    $res = $this->IMG->photo_add_img2($type_id);
    if($res){
      $this->ajaxReturn(array('state'=>1,'msg'=>'上传成功'));
    }
    $this->ajaxReturn(array('state'=>0,'msg'=>'上传失败'));
} 


//查看相册图片
public  function sel_photo_img(){
  $type_id =$_REQUEST['type_id'];
  $pagenow = empty($_REQUEST['p'])?1:$_REQUEST['p'];
  $pagesize = empty($_REQUEST['pagesize'])?8:$_REQUEST['pagesize'];
  if($type_id == ""){
      $this->ajaxReturn(array('state'=>0,'msg'=>'参数传递错误'));
  }
/*  $res = $this->IMG->sel_photo_img($type_id);
  if($res ==""){
     $this->ajaxReturn(array('state'=>0,'msg'=>'该相册还没有图片'));
  }*/
    $data = array();
    $where['u_id'] = $_SESSION['user_id'];
    $where['type_id'] = $type_id;
    $where['is_activity'] = 1;
    $count = D('images')->where($where)->count();
    $pagecount = ceil($count/$pagesize);
    $totalPages = ($pagenow -1)*$pagesize;
    $data['list'] = D('images')->where($where)->limit($totalPages,$pagesize)->order('id desc')->select();
    if(empty($data['list'])){
      $this->ajaxReturn(array('state'=>0,'msg'=>'该相册没有图片'));
    }
    $data['page'] = array('page'=>$pagenow,'pagesize'=>$pagesize,'count'=>$count,'pagecount'=>$pagecount);
    $this->ajaxReturn($data);
    exit;
   }

}
